package com.itheima.es;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * sort排序
 * from+size分页
 * @作者 itcast
 * @创建日期 2020/11/29 9:31
 **/
public class EsDemo04 {
    RestHighLevelClient restHighLevelClient;

    /**
     * 文档的排序
     * @throws IOException
     */
    @Test
    public void documentSort() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 按照指定字段的升序 asc 排序
//        searchSourceBuilder.sort("age");
        // 参数1: 按照哪个字段排序   参数2: 升序还是降序
        searchSourceBuilder.sort("age", SortOrder.DESC);
        searchSourceBuilder.sort("gender", SortOrder.ASC);
//        SortBuilders.fieldSort("gender").order(SortOrder.DESC);
        printDocumentResult(searchSourceBuilder);
    }

    /**
     * 文档的排序
     * @throws IOException
     */
    @Test
    public void documentPage() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 按照指定字段的升序 asc 排序
//        searchSourceBuilder.sort("age");
        // 参数1: 按照哪个字段排序   参数2: 升序还是降序
        searchSourceBuilder.sort("age", SortOrder.DESC);
//        SortBuilders.fieldSort("gender").order(SortOrder.DESC);
        // 分页
        int page = 2; // 第几页
        int pageSize = 3; // 每页显示多少条
        // 分页参数设置
        searchSourceBuilder.from((page - 1)* pageSize);
        searchSourceBuilder.size(pageSize);
        printDocumentResult(searchSourceBuilder);
    }
    /**
     * 将通用的代码封装
     * 外部只需要构建builder条件 传入即可
     * @param builder
     * @throws IOException
     */
    public void printDocumentResult(SearchSourceBuilder builder) throws IOException {
        // 1. 创建搜索请求   SearchRequest
        SearchRequest request = new SearchRequest("user");
        request.source(builder);
        // 2. 客户端执行搜索请求  client.search
        SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 3. 解析响应结果   SearchResponse
        // response.getHits
        SearchHits hits = searchResponse.getHits(); // 查询结果
        // .getToTalHits().value 查询结果总数量
        System.out.println("查询结果数量:" + hits.getTotalHits().value);
        // .getHits  得到文档列表
        SearchHit[] docList = hits.getHits();
        //      遍历文档列表
        for (SearchHit searchHit : docList) {
            //          .getSource 获取文档内容
            String userJson = searchHit.getSourceAsString();
            User user = JSON.parseObject(userJson,User.class);
            System.out.println(user);
        }
        //          .getHighligher 获取高亮处理结果
        // response.getAggregations 获取聚合结果
    }


    @Before
    public void init(){
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        // 要连接的es地址
                        new HttpHost("192.168.200.150",9200,"http")
//                        new HttpHost("192.168.200.151",9200,"http"),   // 如果是集群的话可以写多个
//                        new HttpHost("192.168.200.152",9200,"http")
                )
        );
    }
    @After
    public void close() throws IOException {
        restHighLevelClient.close();
    }
}
